/** 
 * @copyright		all rights reserved.
 * @author			how_fly@yahoo.com
 * @brief			1. read the words,which will be searched
 * 					   in the web-page,from dictionary
 * 					2. search words in web-page, and statistics
 * 					   the frequency, using multi-threads
 * 
 * @version			1.0
 * @date
 * 
 * 
 */

#ifndef _DICT_MANAGER_H_
#define _DICT_MANAGER_H_

#include <pthread.h>
#include <set>
#include <string>
using namespace std;

///the number of threads to search word in web-page
#define DICT_THREADS_COUNT 10
///the max-length of word
#define DICT_WORD_LEN 64

struct page_t;
/**
 * @brief the word from dictionary.
 * 
 * 
 * 
 */ 
struct dict_t
{
	///the word
	char p[DICT_WORD_LEN];
	
	///compare the word for sorting
	bool operator< (const dict_t& dict) const
	{
		return strcmp(p, dict.p);
	}
};
/**
 * @brief 1.read the word from dictionary.
 * 	      2.search the word in each web-page, using multi-threads
 * 
 * 
 * 
 */ 
struct dict_manager_t
{
	///'true' will jump out of the cycle, clear the memory, and quit,
	bool quit;
	
	///store the word by 'set', 
	///and the number is equal to the number of threads
	set<dict_t>  dicts[DICT_THREADS_COUNT];
	
	///the threads for searching the word in web-pages,
	///and the pthread_mutex and the pthread_cond
	list<page_t*> pages[DICT_THREADS_COUNT];	
	pthread_mutex_t lockers[DICT_THREADS_COUNT];
	pthread_cond_t conds[DICT_THREADS_COUNT];
};

/**
 *  @brief 	1.load the dictionary,
 * 			2.start the multi-threads. each thread will search words in web-pages 
 * 			  in one cycle.
 *  @param [in]dict_manager_t 
 *  @return int: 0 is good, -1 is error.
 */ 
int init_dict_manager(dict_manager_t& dict_manager);

/**
 *  @brief 	add one web-page to all threads, which will search word in
 * 			each cycle.
 *  @param [in]page_t	pointer to the page 
 *  @param [in]dict_manager_t
 *  @return int: 0 is good, -1 is error.
 */
int add_page_to_dict_manager(page_t* page, dict_manager_t* dict_manager);


/**
 *  @brief 	dict_manager_t will jump out of the cycle in each thread
 *  @param [in]dict_manager_t 
 *  @return void
 */
void quit_dict_manager(dict_manager_t* dict_manager);

extern dict_manager_t g_dict_manager;
#endif


